<?php

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');

/* 参数 */
$_REQUEST['id']  = isset($_REQUEST['id'])  ? intval($_REQUEST['id'])  : 0; // 商品编号
$_REQUEST['img'] = isset($_REQUEST['img']) ? intval($_REQUEST['img']) : 0; // 图片编号

$group_pro_id = $_REQUEST['id'];

/* 获得促销名称 */
$sql = 'SELECT group_name FROM ' . $os->table('group_promotion') . "WHERE group_promotion_id = '$group_pro_id'";
$promotion_name = $db->getOne($sql);

/* 如果该促销不存在，返回首页 */
if ($promotion_name === false)
{
    os_header("Location: ./\n");

    exit;
}

/* 获得所有的图片 */
$sql = 'SELECT img_id, img_url, thumb_url, img_desc' .
        ' FROM ' . $GLOBALS['os']->table('group_promotion_gallery') .
        " WHERE is_reveal=1 AND group_promotion_id = '$group_pro_id' ORDER BY img_id";
$img_list = $db->getAll($sql);

$img_count = count($img_list);

$gallery = array('goods_name' => htmlspecialchars($promotion_name, ENT_QUOTES), 'list' => array());
if ($img_count == 0)
{
    /* 如果没有图片，返回商品详情页 */
    os_header('Location: index.php \n');
    exit;
}
else
{
    foreach ($img_list AS $key => $img)
    {
        $gallery['list'][] = array(
            'gallery_thumb' => get_image_path($group_pro_id, $img['thumb_url'], true, 'group_promotion'),
            'gallery' => get_image_path($group_pro_id, $img['img_url'], false, 'group_promotion'),
            'img_desc' => $img_list[$key]['img_desc']
        );
    }
}

$smarty->assign('shop_name',  $_CFG['shop_name']);
$smarty->assign('watermark', str_replace('../', './', $_CFG['watermark']));
$smarty->assign('gallery',  $gallery);
$smarty->display('gallery.dwt');

?>